package com.company.aicrawlers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.aicrawlers.entity.CollectTaskQuestionPlatform;
import com.company.aicrawlers.mapper.CollectTaskQuestionPlatformMapper;
import com.company.aicrawlers.service.CollectTaskQuestionPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 问句-平台执行状态 Service实现类
 * 断点续传核心Service
 * 
 * @author AI Crawlers
 * @since 2025-10-10
 */
@Slf4j
@Service
public class CollectTaskQuestionPlatformServiceImpl 
        extends ServiceImpl<CollectTaskQuestionPlatformMapper, CollectTaskQuestionPlatform> 
        implements CollectTaskQuestionPlatformService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchCreateMatrix(Long taskId, List<Long> questionIds, List<String> platforms) {
        List<CollectTaskQuestionPlatform> list = new ArrayList<>();

        // 创建问句×平台笛卡尔积
        for (Long questionId : questionIds) {
            for (String platformName : platforms) {
                CollectTaskQuestionPlatform item = new CollectTaskQuestionPlatform();
                item.setTaskId(taskId);
                item.setQuestionId(questionId);
                item.setPlatformName(platformName);
                item.setExecuteStatus("pending");
                item.setRetryCount(0);
                item.setCreateTime(LocalDateTime.now());
                list.add(item);
            }
        }

        this.saveBatch(list);
        log.info("批量创建执行状态矩阵成功, taskId={}, count={}", taskId, list.size());
    }

    @Override
    public List<Map<String, Object>> getPendingByTaskAndPlatform(Long taskId, String platformName) {
        return this.baseMapper.selectPendingByTaskAndPlatform(taskId, platformName);
    }

    @Override
    public List<Map<String, Object>> getPendingByTask(Long taskId) {
        return this.baseMapper.selectPendingByTask(taskId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateExecuteStatus(Long taskId, Long questionId, String platformName, 
                                     String executeStatus, String errorMessage) {
        LambdaQueryWrapper<CollectTaskQuestionPlatform> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectTaskQuestionPlatform::getTaskId, taskId);
        wrapper.eq(CollectTaskQuestionPlatform::getQuestionId, questionId);
        wrapper.eq(CollectTaskQuestionPlatform::getPlatformName, platformName);

        CollectTaskQuestionPlatform item = this.getOne(wrapper);
        if (item != null) {
            item.setExecuteStatus(executeStatus);
            item.setErrorMessage(errorMessage);

            if ("running".equals(executeStatus) && item.getExecuteTime() == null) {
                item.setExecuteTime(LocalDateTime.now());
            }

            if ("success".equals(executeStatus) || "failed".equals(executeStatus)) {
                item.setCompleteTime(LocalDateTime.now());
            }

            this.updateById(item);
            log.debug("更新执行状态, taskId={}, questionId={}, platform={}, status={}", 
                    taskId, questionId, platformName, executeStatus);
        }
    }
}

